<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>接水果</title>
    <style>
        html, body {
            height: 100%;
            margin: 0;
            background: #000;
            overflow: hidden;
        }
        #content {
            position: fixed;
            width: 800px;
            height: 450px;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
    <script src="./lufylegend-1.10.1.js"></script>
    <script>
        if (LGlobal.mobile || LGlobal.canTouch) {
            LGlobal.stageScale = LStageScaleMode.SHOW_ALL;
		    LSystem.screen(LStage.FULL_SCREEN);
        }
    </script>
</head>
<body>
    <div id="content"></div>
    <script>
        window.onresize = function () {
            var content = document.getElementById("content"),
                sx = document.body.clientWidth / content.clientWidth,
                sy = document.body.clientHeight / content.clientHeight;
            content.style.transform = "scale(" + (sx < sy ? sx : sy) + ")";
        };
        window.onresize();
        
        LInit(40, "content", 800, 450, load);

        var imgData = [
                { name: "bg", path: "./img/bg.jpg" },
                { name: "player", path: "./img/player.png" },
                { name: "item0", path: "./img/item0.png" },
                { name: "item1", path: "./img/item1.png" },
                { name: "item2", path: "./img/item2.png" },
                { name: "item3", path: "./img/item3.png" },
                { name: "item4", path: "./img/item4.png" },
                { name: "item5", path: "./img/item5.png" },
                { name: "item6", path: "./img/item6.png" },
                { name: "item7", path: "./img/item7.png" }
            ], imgList,
            layer = { load: null, bg: null, player: null, item: null, over: null }, 
            player = { p1: null },
            events = {},
            step = { key: 0, speed: 1, speed_ori: 1 },
            text = { hp: 10, hpField: "", point: 0, pointField: "" },
            coordinate = {
                mouse: { x: 0, y: 0, track: false },
                player: { x: 0, y: 0 }
            };

        function load () {
            layer.load = new LoadingSample3();
            addChild(layer.load);
            LLoadManage.load(imgData, function (progress) {
                layer.load.setProgress(progress);
            }, setTimeout.bind(this, gameInit, 1000));
        }

        function gameInit (result) {
            removeChild(layer.load);
            // 封装素材
            imgList = result;

            // 创建图层及模型
            layer.bg = new LSprite(),
            layer.player = new LSprite(),
            layer.item = new LSprite(),
            layer.over = new LSprite(),
            player.p1 = new Player();

            // 填充背景图片到背景图层
            layer.bg.addChild(new LBitmap(new LBitmapData(imgList["bg"])));

            // 将图层放置到背景图层内
            layer.bg.addChild(layer.item);
            layer.bg.addChild(layer.over);

            // 添加人物道具文字模型
            addPlayer();
            addText();

            // 将背景图层添加到画面中
            addChild(layer.bg);

            // 绑定事件
            addEvents();
        }

        function addEvents () {
            events = {
                frameRun: function () {
                    step.key++;
                    step.speed = step.speed_ori + text.point / 20;
                    for (var i = 0; i < layer.item.childList.length; i++) {
                        var item = layer.item.childList[i];
                        item.run();
                        if (item.mode == 0) layer.item.removeChild(item);
                    }
                    if (step.key % (40 - (Math.floor(step.key / 100) > 38 ? 38 : Math.floor(step.key / 100))) == 0) addItem();
                    player.p1.run();
                    updateText();
                    if (text.hp <= 0) {
                        gameOver();
                        return false;
                    }
                },
                mouseMove: function (e) {
                    e = e || events;
                    coordinate.mouse.x = e.selfX,
                    coordinate.mouse.y = e.selfY,
                    coordinate.mouse.track = true;
                },
                mouseDown: function (e) {
                    player.p1.jump = 1;
                },
                keyUp: function (e) {
                    e = e || events;
                    switch (e.keyCode || e.which) {
                        case 37: case 39: case 65: case 68:
                            player.p1.dir = 0;
                            player.p1.animate.setAction(0);
                    }
                },
                keyDown: function (e) {
                    e = e || events;
                    switch (e.keyCode || e.which) {
                        case 37: case 65:
                            coordinate.mouse.track = false;
                            player.p1.dir = 1;
                            player.p1.animate.setAction(1);
                            break;
                        case 39: case 68:
                            coordinate.mouse.track = false;
                            player.p1.dir = 2;
                            player.p1.animate.setAction(2);
                            break;
                        case 38: case 87:
                            if (player.p1.jump == 0) player.p1.jump = 1;
                            break;
                        case 40: case 83:
                            player.p1.jump = 2;
                    }
                }
            };

            layer.bg.addEventListener(LEvent.ENTER_FRAME, events.frameRun);
            layer.bg.addEventListener(LMouseEvent.MOUSE_MOVE, events.mouseMove);
            layer.bg.addEventListener(LMouseEvent.MOUSE_DOWN, events.mouseDown);
            LGlobal.stage.addEventListener(LKeyboardEvent.KEY_UP, events.keyUp);
            LGlobal.stage.addEventListener(LKeyboardEvent.KEY_DOWN, events.keyDown);
        }

        function addPlayer () {
            // 将人物模型添加到人物图层中
            layer.player.addChild(player.p1);

            // 将人物图层添加到背景图层中
            layer.bg.addChild(layer.player);
        }

        function addItem () {
            // 创建新物体对象
            var item = new Item();

            // 物体随机位置
            item.x = Math.floor(Math.random() * LGlobal.width - 50);

            // 将物体对象放到物体图层
            layer.item.addChild(item);
        }

        function addText () {
            text.hpField = new LTextField(),
            text.pointField = new LTextField();
            text.hpField.color = text.pointField.color = "#fff",
            text.hpField.size = text.pointField.size = 22,
            text.hpField.x = text.pointField.x = 10,
            text.hpField.y = 12,
            text.pointField.y = 46;
            updateText();
            layer.bg.addChild(text.hpField),
            layer.bg.addChild(text.pointField);
        }

        function updateText () {
            text.hpField.text = "生命值: " + text.hp,
            text.pointField.text = "积分: " + text.point;
        }

        function gameOver () {
            layer.bg.die();
            layer.item.removeAllChild();
            var overText = new LTextField();
            overText.text = "GAME OVER"
            overText.color = "#f00",
            overText.size = 50,
            overText.x = (LGlobal.width - overText.getWidth()) / 2,
            overText.y = (LGlobal.height - overText.getHeight()) / 2
            layer.over.addChild(overText);
            layer.bg.addEventListener(LMouseEvent.MOUSE_DOWN, function () {
                layer.bg.die();
                layer.over.removeAllChild();
                text.hp = 10, text.point = 0;
                step.key = 0, step.speed = 1;
                player.p1.x = player.p1.y = 350,
                player.p1.dir = 0, player.p1.jump = 0;
                player.p1.animate.setAction(0);
                events = {};
                addEvents();
            });
        }

        function Player() {
            var t = this;
            base(t, LSprite, []);

            // 设置人物的坐标及坐标最小最大值
            t.x_ori = t.y_ori = t.x = t.y = 350;
            t.x_min = 0, t.x_max = LGlobal.width - 64,
            t.y_min = t.y_ori - 60, t.y_max = t.y_ori;

            // 设置人物的方向: 0 正面; 1 向左; 2 向右
            t.dir = 0;

            // 设置人物跳跃状况
            t.jump = 0;

            // 设置精灵图动画: (当前角色, 初始帧画面, 帧画面组)
            t.animate = new LAnimation(
                t,
                new LBitmapData(imgList['player'], 0, 0, 64, 64),
                LGlobal.divideCoordinate(256, 256, 4, 4),
            )

            // 设置精灵图动画每行起始帧列位与最大帧列位
            t.stepIndex = 0, t.stepMax = 3;
        }

        Player.prototype.run = function () {
            var t = this;

            // 更新人物坐标跟踪鼠标位置
            if (coordinate.mouse.track) {
                if (coordinate.mouse.x < t.x + t.getWidth() / 2 - 10) {
                    t.dir = 1;
                    t.animate.setAction(1);
                } else if (coordinate.mouse.x > t.x + t.getWidth() / 2 + 10) {
                    t.dir = 2;
                    t.animate.setAction(2);
                } else {
                    t.dir = 0;
                    t.animate.setAction(0);
                    coordinate.mouse.track = false;
                }
            }

            // 更新人物X轴位移
            var dis = 10 * (step.speed || step.speed_ori);
            if (t.dir == 1) {
                t.x = t.x - dis > t.x_min ? t.x - dis : 0;
            } else if (t.dir == 2) {
                t.x = t.x + dis < t.x_max ? t.x + dis : t.x_max;
            }

            // 更新人物跳跃位移
            if (t.jump == 1) {
                if (t.y - 10 >= t.y_min) {
                    t.y = t.y - 10;
                } else {
                    t.y = t.y_min;
                    t.jump = 2;
                }
            } else if (t.jump == 2) {
                if (t.y + 10 <= t.y_max) {
                    t.y = t.y + 10;
                } else {
                    t.y = t.y_max;
                    t.jump = 0;
                }
            }

            // 更新人物坐标
            coordinate.player.x = t.x,
            coordinate.player.y = t.y;

            // 更新人物帧动画
            if (t.stepIndex + 1 > t.stepMax) {
                t.stepIndex = 0, t.animate.onframe();
            }
            else t.stepIndex++;
        }

        function Item() {
            var t = this;
            base(t, LSprite, []);

            // 随机获取物体的索引
            var index = Math.floor(Math.random() * 8);
            
            // 设置当前物体是加分还是减分
            t.value = index < 4 ? 1 : -1;

            // 物体当前模式: 0 销毁; 1 正常下落
            t.mode = 1;

            // 放置物体图片到物体类中
            t.addChild(new LBitmap(new LBitmapData(imgList["item" + index])));
        }

        Item.prototype.run = function () {
            var t = this;
            
            // 让物体Y轴持续向下
            t.y += 5 * (step.speed || step.speed_ori);

            // 判断物体模式及碰撞情况和加减分情况
            if (t.y > LGlobal.height) t.mode = 0;
            else if (LGlobal.hitTestArc(t, player.p1)) {
                t.mode = 0;
                if (t.value > 0) text.point++;
                else text.hp--;
            }
        }
    </script>
</body>
</html>